package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yxx.annotation.OptLogger;
import com.yxx.annotation.VisitLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.ArticleConditionQuery;
import com.yxx.entityModel.vo.query.TagQuery;
import com.yxx.entityModel.vo.request.TagRequest;
import com.yxx.entityModel.vo.response.ArticleConditionListResponse;
import com.yxx.entityModel.vo.response.TagBackResponse;
import com.yxx.entityModel.vo.response.TagOptionResponse;
import com.yxx.entityModel.vo.response.TagResponse;
import com.yxx.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.yxx.constant.OptTypeConstant.*;

/**
 * 标签控制器
 * @author yuxiangxun
 * @date 2025/2/2
 * @apiNote
 */
@Api(tags = "标签模块")
@RestController
public class TagController {
    @Resource
    private TagService tagService;

    /**
     * 查看后台标签列表
     * @param tagQuery
     * @return
     */
    @ApiOperation(value = "查看后台标签列表")
    @SaCheckPermission("blog:tag:list")
    @GetMapping("/admin/tag/list")
    public ResponseResult<PageResult<TagBackResponse>> tagBackListVO(TagQuery tagQuery){
        return ResponseResult.success(tagService.tagBackListVO(tagQuery));
    }

    /**
     * 添加标签
     * @param tagRequest
     * @return
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加标签")
    @SaCheckPermission("blog:tag:add")
    @PostMapping("/admin/tag/add")
    public ResponseResult<?> addTag(@Validated @RequestBody TagRequest tagRequest){
        tagService.addTag(tagRequest);
        return ResponseResult.success();
    }

    /**
     * 删除标签
     * @param tagIdList
     * @return
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除标签")
    @SaCheckPermission("blog:tag:delete")
    @DeleteMapping("/admin/tag/delete") // fix:修复后台无法删除标签的问题
    public ResponseResult<?> deleteTag(@RequestBody List<Integer> tagIdList){
        tagService.deleteTag(tagIdList);
        return ResponseResult.success();
    }

    /**
     * 修改标签
     * @param tagRequest
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改标签")
    @SaCheckPermission("blog:tag:update")
    @PutMapping("/admin/tag/update")
    public ResponseResult<?> updateTag(@Validated @RequestBody TagRequest tagRequest){
        tagService.updateTag(tagRequest);
        return ResponseResult.success();
    }

    /**
     * 查看标签选项
     * @return
     */
    @ApiOperation(value = "查看标签选项")
    @GetMapping("/admin/tag/option")
    public ResponseResult<List<TagOptionResponse>> TagListOption(){
        return ResponseResult.success(tagService.tagListOption());
    }

    /**
     * 查看标签列表
     * @return
     */
    @ApiOperation(value = "查看标签列表")
    @GetMapping("/tag/list")
    @VisitLogger(value = "文章标签")
    public ResponseResult<List<TagResponse>> TagListVO(){
        return ResponseResult.success(tagService.tagListVO());
    }

    /**
     * 查看标签下的文章列表
     * @param articleConditionQuery
     * @return
     */
    @ApiOperation(value = "查看标签下的文章列表")
    @GetMapping("/tag/article")
    @VisitLogger(value = "标签文章")
    public ResponseResult<ArticleConditionListResponse> listArticleTag(ArticleConditionQuery articleConditionQuery){
        return ResponseResult.success(tagService.articleTagList(articleConditionQuery));
    }
}
